package com.asia5b.a5mall.goods.widget

import android.text.TextUtils
import com.asia5b.a5mall.middleware.model.goods.SelectorBean
import com.asia5b.a5mall.middleware.model.goods.SelectorKeyBean
import java.lang.Exception

/**
 * Created by zhangyuncai on 2020/5/16.
 */
object SearchGoodsHelper {
    fun getSelectorBean(selectorKeyBean: SelectorKeyBean, params: HashMap<String, Any>): SelectorBean? {
        var returnSelectorBean: SelectorBean? = null
        var selectValue = ""
        if (selectorKeyBean.isBrand()) {
            selectValue = "${params.get("brand")}"
        } else if (selectorKeyBean.isCate()) {
            selectValue = "${params.get("category")}"
        } else {
            val prop = "${params.get("prop")}"

            if (!TextUtils.isEmpty(prop)) {
                val propStringList = prop.split("@")
                for (s in propStringList) {
                    if (s.contains(selectorKeyBean.key)) {
                        try {
                            selectValue = s.split("_")[1]
                        } catch (e: Exception) {
                            e.printStackTrace()
                            selectValue = ""
                        }
                    }
                }
            }

        }
        if (!TextUtils.isEmpty(selectValue)) {
            val valueList = selectorKeyBean.value!!
            for (selectorBean in valueList) {
                if (TextUtils.equals(selectValue, selectorBean.value)) {
                    returnSelectorBean = selectorBean
                    break
                }
            }
        }
        return returnSelectorBean

    }

    fun getSelectValue(selectorKeyBean: SelectorKeyBean, params: HashMap<String, Any>): String? {
        var selectValue: String? = null
        val valueList = selectorKeyBean.value!!
        if (selectorKeyBean.isBrand()) {//品牌
            if (params.containsKey("brand")) {
                for (selectorBean in valueList) {
                    if (TextUtils.equals("${params.get("brand")}", selectorBean.value)) {
                        selectValue = selectorBean.name
                        break
                    }
                }
            }
        } else if (selectorKeyBean.isCate()) {//分类
            if (params.containsKey("category")) {
                for (selectorBean in valueList) {
                    if (TextUtils.equals("${params.get("category")}", selectorBean.value)) {
                        selectValue = selectorBean.name
                        break
                    }
                }
            }
        } else {//其他
            if (params.containsKey("prop")) {
                val prop = "${params.get("prop")}"
                val propStringList = prop.split("@")
                for (key_value in propStringList) {
                    for (selectorBean in valueList) {
                        if (key_value.contains(selectorBean.value)) {
                            selectValue = selectorBean.name
                            return selectValue
                        }
                    }
                }
            }
        }

        return selectValue
    }

}